//
//  KLMAIModeCell.swift
//  KLM_GN
//
//  Created by Nic on 2025/3/5.
//

import UIKit

struct KLMGradient {
    let colors: [UIColor]       // 渐变色数组
    let locations: [CGFloat]?   // 颜色位置（可选，范围 0.0 ~ 1.0）
    let startPoint: CGPoint     // 渐变起点（相对坐标，如 (0, 0) 表示左上角）
    let endPoint: CGPoint       // 渐变终点（相对坐标，如 (1, 1) 表示右下角）
}

protocol KLMAIModeCellDelegate: AnyObject{
    func aiModeCellToggleOn(cell: KLMAIModeCell, index: IndexPath) -> Void
}

class KLMAIModeCell: UICollectionViewCell {

    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var detailLabel: UILabel!
    @IBOutlet weak var onOffBtn: UIButton!
    @IBOutlet weak var bgImgView: UIImageView!
    @IBOutlet weak var iconView: UIButton!
    
    var index: IndexPath = []
    var collectionView: UICollectionView?
    
    
    var cellCard : KLMCard! {
        didSet {
            if cellCard.type == .aiMode {
                nameLabel.configStyle(with: cellCard.title)
                detailLabel.configStyle(with: cellCard.subTitle)
                
                let img = cellCard.isOn ? "switch_on" : "switch_off"
                onOffBtn.setImage(UIImage(named: img), for: .normal)
            }
        }
    }
    func updateIcon(rkScene: KLMRKScene) {
        var icon = "icon_Work"
        if let codeType = rkScene.rkSceneCodeType {
            if codeType == .rk_computer {
                icon = "icon_Work"
            } else if codeType == .rk_conserve {
                icon = "icon_EnergySaving"
            } else if codeType == .rk_electronics {
                icon = "icon_Reading"
            } else if codeType == .rk_rest_state {
                icon = "icon_rest"
            }
        }
        if !cellCard.isOn {
            icon += "_s"
        }
        iconView.setImage(UIImage(named: icon), for: .normal)
    }
    // icon_EnergySaving  icon_Reading  icon_rest  icon_Work
    
    var delegate: KLMAIModeCellDelegate?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        nameLabel.configStyle(font: UIFont(safeName: "PingFangSC-Medium", size: 16), titleColor: rgba(38, 38, 38, 1))
        detailLabel.configStyle(font: UIFont(safeName: "PingFangSC-Regular", size: 12), titleColor: rgba(129, 129, 129, 1))
        layer.shadowColor = rgba(0, 0, 0, 0.18).cgColor
        layer.shadowOffset = CGSize(width: 0, height: 2)
        layer.shadowOpacity = 0.4
        layer.shadowRadius = 6
        layer.masksToBounds = false
        iconView.isUserInteractionEnabled = false
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        
        if cellCard.isOn {
            bgImgView.image = UIImage(named: "AI_Mode_Bg")
//            bgImgView.image = UIImage.image(gradient: KLMGradient(colors: [rgba(235, 250, 241, 1), rgba(203, 242, 219, 1), rgba(118, 219, 157, 1)], locations: [0.0, 0.43, 1.0], startPoint: CGPointMake(0, 0.7), endPoint: CGPointMake(1, 0.3)), size: contentView.size, cornerRadius: 8, borderColor: rgba(19, 207, 128, 1), borderWidth: 1)
        } else {
            bgImgView.image = UIImage.image(withColor: rgba(255, 255, 255, 1), size: contentView.size, cornerRadius: 8, borderColor: rgba(222, 222, 222, 1), borderWidth: 0.5)
            //rgba(233, 233, 233, 1)
        }
    }
    static func cell(with collectionView: UICollectionView, at indexPath: IndexPath, cellId: String) -> Self {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! Self
        cell.index = indexPath
        cell.collectionView = collectionView
        return cell
    }
    @IBAction func tapOnOffBtn(_ sender: UIButton) {
        delegate?.aiModeCellToggleOn(cell: self, index: index)
    }
}
